package com.qyer.lxp.order.service.impl;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.qyer.lxp.common.date.DateUtil;
import com.qyer.lxp.common.framework.contants.ClientErrorMessageEnum;
import com.qyer.lxp.common.framework.contants.QyerContants;
import com.qyer.lxp.common.framework.exception.QyerClientException;
import com.qyer.lxp.common.framework.util.Md5Encrypt;
import com.qyer.lxp.common.framework.util.QyerConfig;
import com.qyer.lxp.common.framework.util.QyerUtils;
import com.qyer.lxp.model.Response;
import com.qyer.lxp.model.availability.normal.Available;
import com.qyer.lxp.model.availability.parameter.AvailabilityRequest;
import com.qyer.lxp.model.availability.parameter.BookedSpecification;
import com.qyer.lxp.model.orderstatus.normal.OrderStatus;
import com.qyer.lxp.model.orderstatus.normal.OrderStatuses;
import com.qyer.lxp.model.orderstatus.parameter.OrderStatusRequest;
import com.qyer.lxp.order.model.AvailableParam;
import com.qyer.lxp.order.model.OrderStatusParam;
import com.qyer.lxp.order.service.IBookProvideWebService;
import com.qyer.lxp.product.model.ProductParam;
import com.qyer.lxp.service.order.IBookProvideService;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wangzhen on 16/6/24.
 */
@Path("/")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class BookProvideWebServiceImpl implements IBookProvideWebService {

    @Resource
    private IBookProvideService provideService;

    /**
     * 预定限制接口
     *
     * @param param
     * @return
     * @throws Exception
     */
    @POST
    @Path("/booklimit")
    public Response<Map<String, Object>> getBookLimit(ProductParam param) throws Exception {
        Response<Map<String, Object>> response = new Response<Map<String, Object>>();
        if (null == param) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        String secretKey = QyerConfig.getInstance().getString(QyerContants.LIXING_SECRET_KEY);
        String token = Md5Encrypt.md5("param:" + param.getSaleCode() + param.getTimestamp() + param.getSupplierid() + secretKey);

        if (token.equals(param.getToken())) {
            if ("Lixing".equals(param.getSupplierid())) {
                Map<String, Object> booklimits = provideService.getBookLimit(param.getSaleCode());
                response.setNameSpace("booklimit");
                response.setData(booklimits);
            } else {
                throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_507.getCode());
            }

        } else {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_506.getCode());
        }


        return response;

    }

    /**
     * 验价接口
     *
     * @param param
     * @return
     * @throws Exception
     */
    @POST
    @Path("/getAvailablity")
    public Response<Available> getAvailablity(AvailableParam param) throws Exception {
        Response<Available> response = new Response<Available>();
        if (null == param) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        String secretKey = QyerConfig.getInstance().getString(QyerContants.LIXING_SECRET_KEY);
        String token = Md5Encrypt.md5("param:" + param.getSaleCode() + param.getTimestamp() + param.getSupplierid() + secretKey);

        if (token.equals(param.getToken())) {
            if (QyerUtils.isNotEmpty(param.getCodeSpecficationId()) && QyerUtils.isNotEmpty(param.getTravelDate()) && QyerUtils.isNotEmpty(param.getNum())) {
                if ("Lixing".equals(param.getSupplierid())) {
                    AvailabilityRequest request = new AvailabilityRequest();
                    request.setCurrency("CNY");
                    request.setSaleCode(param.getSaleCode());
                    request.setTravelDate(DateUtil.convertDate(param.getTravelDate()));
                    if (QyerUtils.isNotEmpty(param.getEndDate())) {
                        request.setEndDate(DateUtil.convertDate(param.getEndDate()));
                    }
                    String specficationId = param.getCodeSpecficationId().replace(param.getSaleCode(), "");
                    BookedSpecification sp = new BookedSpecification();
                    sp.setSpecification(specficationId);
                    sp.setNum(Integer.valueOf(param.getNum()));
                    List<BookedSpecification> list = new ArrayList<BookedSpecification>();
                    list.add(sp);
                    request.setBookedSpecifications(list);
                    Available availablity = provideService.getAvailablity(request);
                    response.setNameSpace("getAvailablity");
                    response.setData(availablity);

                } else {
                    throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_507.getCode());
                }
            } else {
                throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_508.getCode());
            }


        } else {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_506.getCode());
        }


        return response;

    }


    /**
     * 查询订单状态接口
     *
     * @param param
     * @return
     * @throws Exception
     */
    @POST
    @Path("/getOrderStatus")
    public Response<List<OrderStatus>> getOrderStatus(OrderStatusParam param) throws Exception {
        Response<List<OrderStatus>> response = new Response<List<OrderStatus>>();
        if (null == param) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        String secretKey = QyerConfig.getInstance().getString(QyerContants.LIXING_SECRET_KEY);
        String token = Md5Encrypt.md5("param:" + param.getOrderCd() + param.getTimestamp() + param.getSupplierid() + secretKey);

        if (token.equals(param.getToken())) {
            if (QyerUtils.isNotEmpty(param.getOrderCd())) {
                if ("Lixing".equals(param.getSupplierid())) {
                    OrderStatusRequest request = new OrderStatusRequest();
                    List<String> listOrderCd = new ArrayList<String>();
                    listOrderCd.add(param.getOrderCd());
                    request.setOrderCds(listOrderCd);
                    if (QyerUtils.isNotEmpty(param.getRefOrderCd())) {
                        List<String> listOrderRefer = new ArrayList<String>();
                        listOrderRefer.add(param.getRefOrderCd());
                        request.setRefOrderCds(listOrderRefer);
                    }

                    List<OrderStatus> orderStatus = provideService.getOrderStatus(request);
                    response.setNameSpace("getOrderStatus");
                    response.setData(orderStatus);

                } else {
                    throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_507.getCode());
                }
            } else {
                throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_508.getCode());
            }


        } else {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_506.getCode());
        }


        return response;

    }

}
